package Graph;

import java.util.*;

public class 孤岛的总面积 {
    static int n = 0;
    static int m = 0;
    static int[][] map;

    public static void main (String[] args) {
        Scanner in = new Scanner(System.in);
        n = in.nextInt();
        m = in.nextInt();
        map = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                map[i][j] = in.nextInt();
            }
        }
        for (int i = 0; i < n; i += n - 1) {
            for (int j = 0; j < m; j++) {
                if (map[i][j] == 1) dfs(i, j);
            }
        }
        for (int i = 1; i < n - 1; i++) {
            for (int j = 0; j < m; j += m - 1) {
                if (map[i][j] == 1) dfs(i, j);
            }
        }
        int res = 0;
        for (int i = 1; i < n - 1; i++) {
            for (int j = 1; j < m - 1; j++) {
                if (map[i][j] == 1) res += dfs(i, j);
            }
        }
        System.out.println(res);
    }

    private static int dfs(int r, int c) {
        if (r < 0 || r >= n || c < 0 || c >= m || map[r][c] == 0) {
            return 0;
        }
        map[r][c] = 0;
        return 1 + dfs(r + 1, c) + dfs(r, c + 1) + dfs(r - 1, c) + dfs(r, c - 1);
    }
}
